// 索引数据, dynamic mapping 会不断加入新的字段

PUT cookie_service/_doc/1
{
 "url":"www.google.com",
 "cookies":{
   "username":"tom",
   "age":32
 }
}

PUT cookie_service/_doc/2
{
 "url":"www.amazon.com",
 "cookies":{
   "login":"2019-01-01",
   "email":"xyz@abc.com"
 }
}

DELETE cookie_service

// 使用nested 对象, 增加key/value
PUT cookie_service
{
  "mappings": {
    "properties": {
      "cookies": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "dateValue": {
            "type": "date"
          },
          "keywordValue": {
            "type": "keyword"
          },
          "IntValue": {
            "type": "integer"
          }
        }
      },
      "url": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

// 写入数据，使用key和合适类型的value字段
PUT cookie_service/_doc/1
{
    "url": "www.google.com",
    "cookies": [
        {
            "name": "username",
            "keywordValue": "tom"
        },
        {
            "name": "age",
            "intValue": 32
        }
    ]
}

PUT cookie_service/_doc/2
{
    "url": "www.amazon.com",
    "cookies": [
        {
            "name": "login",
            "dateValue": "2019-01-01"
        },
        {
            "name": "email",
            "IntValue": 32
        }
    ]
}

// Nested 查询，通过bool查询进行过滤
POST cookie_service/_search
{
    "query": {
        "nested": {
            "path": "cookies",
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "cookies.name": "age"
                            }
                        },
                        {
                            "range": {
                                "cookies.intValue": {
                                    "gte": 30
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}
